<?php
class cls_data_permission extends cls_data_base
{
	var $table_name = '`permission`';
	
	function get_permission_list()
	{
		$sql = "select p.*, group_concat(a.user_name) user_list
				from {$this->table_name} p
					left join allocation_permission ap on ap.permission_id = p.permission_id and is_valid = 1
					left join administor a on a.administor_id = ap.administor_id
				group by p.permission_id
			";
		$list = $this->db->getAll($sql);
		$ret = array();
		foreach ($list as $v)
		{
			$ret[$v['permission_id']] = $v;
		}
		return $ret;
	}
	
	function alloc_permission($user_name, $list, $is_valid)
	{
		$user_name = mysql_real_escape_string($user_name);
		if (empty($list)) {
			return;
		}
		foreach ($list as $k => $v) {
			$list[$k] = "'" . mysql_real_escape_string($v) . "'";
		}
		$permission_str = implode(',', $list);
		$sql = "select permission_id from {$this->table_name} where permission_name in ({$permission_str})";
		$permission_list = $this->db->getCol($sql);
		$sql = "select administor_id from administor where user_name = '{$user_name}'";
		$administor_id = $this->db->getOne($sql);
		$time = get_now();
		
		$value = array();
		foreach ($permission_list as $permission_id) {
			$value[] = "({$administor_id},{$permission_id},'{$time}',{$is_valid})";
		}
		$value_str = implode(',', $value);
		$sql = "replace allocation_permission(administor_id, permission_id, update_time, is_valid) 
				value {$value_str}";
		$this->db->query($sql);
	}
}